package com.egoo.ticket.server.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.egoo.ticket.server.config.mq.RabbitMqConfig;
import com.egoo.ticket.server.dao.*;
import com.egoo.ticket.server.pojo.dto.WorkOrderUrgeInputDto;
import com.egoo.ticket.server.pojo.dto.WorkOrderUrgeOutDto;
import com.egoo.ticket.server.pojo.entity.*;
import com.egoo.ticket.server.service.EripDataSynService;
import com.egoo.ticket.server.service.WorkOrderUrgeService;
import com.egoo.ticket.server.utils.common.ConstantCode;
import com.egoo.ticket.server.utils.common.CreateCodeUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.sql.Timestamp;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author Administrator
 */
@Service
@Slf4j
@Transactional(rollbackFor = Exception.class)
public class WorkOrderUrgeServiceImpl implements WorkOrderUrgeService {
	@Autowired
	private CreateCodeUtil createCodeUtil;
	@Autowired
	EwsUserDao ewsUserDao;
	@Autowired
	WorkOrderUrgeDao workOrderUrgeDao;
	@Autowired
	WorkOrderDao workOrderDao;
	@Autowired
	WorkOrderLogDao workOrderLogDao;
	@Autowired
	EripDataSynService eripDataSynService;
	@Autowired
	private WorkOrderRelationMapper workOrderRelationMapper;
	@Autowired
	private AmqpTemplate amqpTemplate;
	@Autowired
	private RabbitMqConfig rabbitMqConfig;



	/**
	 * 根据工单Id查询相关催单信息
	 *
	 * @param workOrderId
	 * @return
	 */
	@Override
	public List<WorkOrderUrgeOutDto> selectByWorkOrderId(String workOrderId) {
		List<WorkOrderUrgeOutDto> workOrderUrgeList = workOrderUrgeDao.selectByWorkOrderId(workOrderId);
		return workOrderUrgeList;
	}

	/**
	 * 批量催单
	 * 
	 * @param workOrderUrgeInputDto
	 * @return
	 */
	@Override
	public Integer insertWorkOrderUrgeBatch(WorkOrderUrgeInputDto workOrderUrgeInputDto) {
		String userId = workOrderUrgeInputDto.getUserId();
		String tenantId = workOrderUrgeInputDto.getTenantId();
		EwsUser ewsUser = ewsUserDao.selectById(workOrderUrgeInputDto.getUserId());
		String ewsUserName = ewsUser.getName();
		// 批量添加催单记录
		List<WorkOrderUrge> workOrderUrgeList = new ArrayList<>();
		// 批量添加催单日志
		List<WorkOrderLog> workOrderLogList = new ArrayList<WorkOrderLog>();

		for (int i = 0; i < workOrderUrgeInputDto.getWorkOrderIdList().size(); i++) {
			String workOrderId = workOrderUrgeInputDto.getWorkOrderIdList().get(i);
			WorkOrder workOrder = workOrderDao.selectById(workOrderId);
			WorkOrderRelation workOrderRelation = getByWorkOrderId(workOrderId);
			String currentHandlerId = workOrder.getCurrentHandlerId();
			if (!currentHandlerId.contains(userId)) {
				// 更新催单次数,修改催单状态
				workOrderRelation.setUrgeCount(workOrderRelation.getUrgeCount() + 1);
				workOrderRelationMapper.updateWorkOrderRelation(workOrderRelation);
				WorkOrderUrge workOrderUrge = new WorkOrderUrge();
				workOrderUrge.setContent(workOrderUrgeInputDto.getContent());
				workOrderUrge.setStatus(0);
				workOrderUrge.setUserId(workOrderUrgeInputDto.getUserId());
				workOrderUrge.setUserName(ewsUserName);
				Timestamp time = new Timestamp(System.currentTimeMillis());
				workOrderUrge.setCreateTime(time);
				workOrderUrge.setWorkOrderId(workOrderId);
				workOrderUrge.setTenantId(tenantId);
				workOrderUrgeList.add(workOrderUrge);

				// 添加催单日志
				WorkOrderLog workOrderLog = addWorkOrderLogInfo(ewsUser, workOrder, workOrderUrgeInputDto.getContent());
				workOrderLogList.add(workOrderLog);
			}
		}

		log.info("催单日志记录数据：" + workOrderLogList);
		Integer logResult = workOrderLogDao.insertBatch(workOrderLogList);
		log.info("催单日志记录成功条数：" + logResult);

		Integer result = workOrderUrgeDao.insertWorkOrderUrgeBatch(workOrderUrgeList);
		if(result>0){
			for (String id:workOrderUrgeInputDto.getWorkOrderIdList()) {
				JSONObject jsonObject = new JSONObject();
				jsonObject.put(ConstantCode.MsgParam.WORK_ORDER_ID,id);
				jsonObject.put(ConstantCode.MsgParam.MSG_TARGET_SIGN,ConstantCode.SysDictCode.SEND_MSG_CURRENT_HANDLER);
				jsonObject.put(ConstantCode.MsgParam.MSG_TYPE,ConstantCode.WorkOrderLogType.LOG_TYPE_URGE);
				String mqContent = jsonObject.toJSONString();
//				String mqContent= id+"-"+ConstantCode.SysDictCode.SEND_MSG_CURRENT_HANDLER;
				amqpTemplate.convertAndSend(rabbitMqConfig.WORKORDER_SHORTMSG,mqContent);

				//		通知设置调试完成可放开
				JSONObject jsonObjectGeneral = new JSONObject();
				jsonObjectGeneral.put(ConstantCode.MsgParam.WORK_ORDER_ID,id);
				jsonObjectGeneral.put(ConstantCode.MsgParam.MSG_NOTIFY_TYPE, ConstantCode.MsgNotifyType.URGE_NOTIFY);
				amqpTemplate.convertAndSend(rabbitMqConfig.WORKORDER_GENERAL,jsonObjectGeneral.toJSONString());
				log.info("催单mq消息存放成功"+jsonObjectGeneral);
			}
		}
		return result;
	}

	/**
	 * @Author: Donny_dong
	 * @Description: 根据工单id获取唯一个工单关系对象
	 * @Param:
	 * @return:
	 * @Date: 2020/4/26
	 */
	public WorkOrderRelation getByWorkOrderId(String workOrderId) {
		WorkOrderRelation workOrderRelation = new WorkOrderRelation();
		workOrderRelation.setWorkOrderId(workOrderId);
		workOrderRelation = workOrderRelationMapper.selectWorkOrderRelation(workOrderRelation);
		return workOrderRelation;
	}
	/**
	 * TODO 组装工单日志信息
	 * 
	 * @param workOrder
	 * @param
	 * @return
	 */
	private WorkOrderLog addWorkOrderLogInfo(EwsUser ewsUser, WorkOrder workOrder, String content) {
		WorkOrderRelation workOrderRelation = getByWorkOrderId(workOrder.getId());
		WorkOrderLog workOrderLog = new WorkOrderLog();
		workOrderLog.setId(createCodeUtil.getWorkOrderLogId());
		workOrderLog.setLogType(ConstantCode.WorkOrderLogType.LOG_TYPE_REMINDER);
		workOrderLog.setContent(content);
		workOrderLog.setLogCreateTime(new Date());
		workOrderLog.setLogCreaterId(ewsUser.getId());
		workOrderLog.setLogCreaterName(ewsUser.getName());
		workOrderLog.setWorkOrderId(workOrder.getId());
		workOrderLog.setWorkOrderStatusId(workOrder.getStatusId());
		workOrderLog.setWorkOrderNodeId(workOrder.getCurrentWorkOrderNodeId());
		workOrderLog.setWorkOrderNodeName(workOrder.getCurrentWorkOrderNodeName());
		workOrderLog.setTenantId(workOrder.getTenantId());
		return workOrderLog;
	}

	/**
	 * 根据催单id查询催单信息
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public WorkOrderUrge selectById(Integer id) {
		WorkOrderUrge workOrderUrge = workOrderUrgeDao.selectById(id);
		log.info("selectById: {}", workOrderUrge);
		return workOrderUrge;
	}

	/**
	 * 关闭催办信息
	 * 
	 * @param id
	 * @return
	 */
	@Override
	public Integer closeWorkOrderUrge(Integer id) {
		WorkOrderUrge workOrderUrge = workOrderUrgeDao.selectById(id);
		Integer result = workOrderUrgeDao.closeWorkOrderUrge(workOrderUrge);
		log.info("closeWorkOrderUrge: {}", workOrderUrge);
		return result;
	}
}
